#*------coding=utf-8-------------
from base import *
from google.appengine.api import memcache
g_tmp={} #为闭包函数使用
class Cnbeta(BaseRequestHandler):
        def ArtGet(self,req_a,req_pic):
                if not req_a.isdigit():  #iput check
                        return
                #先抓网页,抓不到再尝试使用缓存
                fetch_result=Gae.urlfind('http://cnbeta.com/articles/%s.htm'%req_a,'<h3 id="news_title">(.*?)</.*?<div id="news_content">(.*?)<div class="digbox">.*?<div id="unit_long_story.*?<a href="/rating.*?&s=(.*?)"',False)  #提取正文
                #r=Gae.url('http://www.cnbeta.com/api/getNewsContent.php?articleId=%s'%req_a)
                #   http://ie.cnbeta.com/api/cnBeta.php?m=getNewsDetail&id=225144
                if len(fetch_result)<1:#fetch check 
                        self.write(memcache.get(req_a+'a'))
                        return
                #返回三个 分别为 0 标题 1 正文 2 评分相关
                memcache.set(req_a+'r',fetch_result[2],600) #缓存10分钟 评分数据
                fetch_result[1]=Gae.str2u(fetch_result[1][fetch_result[1].find('<p>'):]) #编码转换
                #picture
                #
                art_content=re.sub('<img([^>]*?)src="http://img.cnbeta.com/(.*?)"(.*?)>', lambda x:'<img%ssrc="cb?%s"%s>'%(x.group(1),urllib.urlencode({'im':x.group(2)}),x.group(3)), fetch_result[1]) #图片外联
                art_content=re.sub('<img([^>]*?)src="pic(.*?)"(.*?)>', lambda x:'<img%ssrc="http://cnbeta.com/pic%s"%s>'%(x.group(1),x.group(2),x.group(3)), art_content) #cb自身图片
                #check if show pic it will affect video
                if req_pic=='0':
                        art_content=re.sub('<img (.*?)>', '', art_content) #图片
                        art_content=re.sub('(<embed.*?>)','', art_content) #flash
                else:
                        art_content=re.sub('<img (.*?)>', lambda x:'<img onload="pic_size(this)" %s>'%x.group(1), art_content) #图片大小
                        art_content=re.sub('class=".*?"', '', art_content)
                art_content=re.sub('style=".*?"','', art_content)
                art_content=re.sub('alt=".*?"', '', art_content)
                art_content=re.sub('(?s)<div[^>]*?>(.*?)</div>', lambda x:x.group(1), art_content)
                art_content=re.sub('(?s)<span[^>]*?>(.*?)</span>', lambda x:x.group(1), art_content)
                art_content=re.sub('(?s)<a[^>]*?href="[^"]*?".*?>(.*?)</a>',lambda x:x.group(1),art_content)
                #一系列的流量优化
                #if ip ,but android above 4.0 also not support flash
                #对 flash 的进一步处理
                if ('iPhone' in self.headers['User-Agent'] or 'iPad' in self.headers['User-Agent']) and '<embed' in art_content:
                        global g_tmp
                        # 从这里抓取 iOs 支持的 m4u 格式文件视频
                        g_tmp['r']=Gae.urlfind('http://www.cnbeta.com/api/getNewsContent2.php?articleId=%s'%req_a,'(<video.*?>)')
                        g_tmp['x']=0
                        def A(z):
                        # x 用于记录当前是第几个要处理的flash 标签 
                                global g_tmp
                                g_tmp['x']=g_tmp['x']+1
                                return g_tmp['r'][g_tmp['x']-1]
                        try:
                                art_content=re.sub('(<embed.*?>)',A, art_content)
                        except:
                                art_content=Gae.url('http://www.cnbeta.com/api/getNewsContent2.php?articleId=%s'%req_a)
                elif  '<embed' in art_content: #视频宽度
                        art_content=re.sub('(<embed.*?>)',lambda x:x.group(1).replace('width','no'), art_content)
                #if req_ajax=='1':
                memcache.set(req_a+'a',co,3600*24*30) #缓存文章
                self.write(art_content)
                return
                #c={'title':Gae.str2u(r[0]),'curname':'文章','nextname':'评论','nextpage':'cb?c=%s'%req_a,'prename':'返回','prepage':'cb','art_content':co}
                #self.render2('cb_article.html',c)
        def GetPic(self,req_im):
                self.response.headers['Content-Type']='image/png'
                mem=memcache.get(req_im)
                if mem:
                        self.write(mem)
                        return		
                img=Gae.url('http://img.cnbeta.com/'+req_im,headers={'Referer':'http://www.cnbeta.com'})
                memcache.set(req_im,img,3600*24*30)
                self.write(img)
        def GetRec(self,req_t):#获取推荐文章
                # http://ie.cnbeta.com/api/cnBeta.php?m=getTopTen
                self.response.headers['Cache-Control']='public, max-age=3600'
                #抓取 top10 文章
                arts=Gae.urljson('http://www.cnbeta.com/api/getTop10.php')
                if len(arts)<2:
                        self.write(memcache.get('tj'))
                        return
                #替换成我们的json格式
                ret_json=[{'t':art['title'],'i':int(art['ArticleID'])} for art in arts]
                ret=json.dumps(ret_json,ensure_ascii=False)
                memcache.set('tj',ret,3600*24)
                self.write(ret)
                return
        def GetHM(self,req_com): #获取精彩评论
                # http://ie.cnbeta.com/api/cnBeta.php?m=getGoodComment
                self.response.headers['Cache-Control']='public, max-age=3600'
                arts=Gae.urljson('http://www.cnbeta.com/api/getHMComment.php?limit=10')
                if len(arts)<2:
                        self.write(memcache.get('jc'))
                        return
                ret_json=[{'t':u'%s<br />出自:%s'%(art["comment"],art["title"]),"i":int(art["ArticleID"])} for art in arts]
                ret=json.dumps(ret_json,ensure_ascii=False)
                memcache.set('tj',ret,3600*24)
                self.write(ret)
                return
        def GetComment(self,req_c):
                # http://www.cnbeta.com/api/getComment.php?article=225132
                re_comment='(fb_content">.*?</dd.*?)?re_detail.*?>([^<].*?)</dd>.*?id="support(.*?)">(.*?)</.*?id="against.*?>(.*?)<'
                # 0 引用? 1 评论内容 2 评论的编号 3 支持数 4 反对数
                self.response.headers['Cache-Control']='no-store'
                if not req_c.isdigit():
                        return
                #c={'arts':[]}
                #c={'title':'评论','curname':'评论','prename':'首页','prepage':'cb','nextname':'发评','nextpage':'cb?sc=%s'%req_c,"comment":1,'arts':[]}
                ret_json=[]
                comments=Gae.urlfind('http://cnbeta.com/comment/normal/%s.html'%req_c,re_comment,str_filter=lambda x:x.replace('<br>',''))
                # wap ver is out
                #if len(arts)<1: #利用手机版
                #        arts=Gae.urlfind('http://m.cnbeta.com/mcomment.php?sid='+req_c,'(5)?</span><br/>([^<]*?)<span >.*?style="color:#bc(.*?);.*?\\(<span >(.*?)<.*?\\(<span >(.*?)<')
                if len(comments)<1: #利用cb月光宝盒
                        result=Gae.url('http://yueguang.sinaapp.com/?id='+req_c)
                        if not reslut:
                                self.write('[]')
                                return
                        sp=result.split('--pushudefengexian--')
                        if len(sp)<2:
                                self.write('[]')
                                return
                        self.response.headers['Cache-Control']='public, max-age=2592000'
                        comments=Gae.refind(re_comment,sp[1].replace('<br>',''))
                if len(comments)<1: #pad ver
                        result=Gae.urljson('http://ie.cnbeta.com/api/cnBeta.php?m=getNewsComment&id=%s'%req_c)
                        comments=[['',r['comment'],r['tid'],r['support'],r['against']] for r in result]
                if len(comments)==0:
                        self.response.headers['Cache-Control']='no-store'
                        self.write(memcache.get(req_c+'c'))
                        return
                for comment in comments[0:80]:
                        comment_content=''
                        comment_json={'i':[int(comment[2]),int(comment[3]),int(comment[4])]}
                        comment_content=(comment[1]).replace('\r','').replace('\n','').replace('\t','').strip() #回车空格替换
                        comment_json['t']=Gae.unescape(comment_json).replace('\r','').replace('\n','').replace('\t','').strip()
                        if type(comment_json['t'])!=type(u''): #评论内容的编码转换
                                try:
                                        comment_json['t']=comment_json['t'].decode('utf8').encode('gbk').decode('gbk')
                                except:
                                        Gae.logE(comment_json['t'])
                                        comment_json['t']=u'无法解码字符'
                        if comment[0]!='':###有引用信息
                                try:
                                        quote=re.search('>([^<]*?)</div>.{0,5}</fieldset>.{0,5}</dd',comment[0],flags=re.DOTALL).group(1)
                                except:
                                        Gae.logE(comment[0])
                                        continue
                        quote=quote.replace('\r','').replace('\n','').replace('\t','').strip()
                        comment_json['t']=u'引(%s)%s'%(Gae.unescape(quote)[0:3],comment_json['t'])
                        comment_json['t']=comment_json['t'].replace('<','').replace('>','')
                        ret_json.append(comment_json)
                ret=json.dumps(ret_json,ensure_ascii=False)
                memcache.set(req_c+'c',ret,6000)
                self.write(ret)
        def get(self): #分发
                self.response.headers['Cache-Control']='public, max-age=2592000'
                req_v=self.getquery('v')
                req_tid=self.getquery('tid')
                req_gv=self.getquery('gv')
                req_supp=self.getquery('supp')
                req_aga=self.getquery('aga')
                req_t=self.getquery('t')        #推荐
                req_a=self.getquery('a')        #文章
                req_c=self.getquery('c')
                req_hc=self.getquery('hc')      #hot comment
                req_com=self.getquery('com')
                req_rate=self.getquery('rate')
                req_im=self.getquery('im')
                #req_ajax=self.getquery('ajax')
                req_ajax='1'
                req_pic=self.getquery('pic')
                if req_a!='':
                        self.ArtGet(req_a,req_pic)
                elif req_im!='': #图片转发
                        self.GetPic(req_im)
                elif req_gv!='': #验证码
                        self.response.headers['Cache-Control']='no-cache'
                        self.response.headers['Content-Type']='image/png'
                        x=Gae.url('http://www.cnbeta.com/validate1.php',headers={'Referer':'http://www.cnbeta.com','Cookie':'PHPSESSID=1'})
                        self.write(x)
                elif req_t!='':#推荐
                        self.GetRec(req_t)
                elif req_com!='':#精彩评论
                        self.GetHM(req_com)
                elif req_rate!='': #评分
                        if not req_rate.isdigit():
                                return
                        if req_ajax=='5':
                                rank='5'
                        else:
                                rank='-5'
                        Gae.url('http://cnbeta.com/rpc.php?j=%s&q=%s&c=11&s=%s'%(req_rate,rank,'1'))
                        mem=memcache.get(req_rate+'r')
                        if mem:
                                Gae.url('http://cnbeta.com/rpc.php?j=%s&q=%s&c=11&s=%s'%(req_rate,rank,mem))
                elif req_c!='':#评论获取
                        self.GetComment(req_c)
                elif req_v!='': #用于更多文章列表获取
                        if not req_v.isdigit():
                                return
                        try:
                                arts=[{"title":art["title"]+'('+{'0':art["cmtnum"],'1':u'关'}[art["cmtClosed"]]+')',"id":int(art["ArticleID"])} for art in Gae.urljson('http://www.cnbeta.com/api/getNewsList.php?fromArticleId=%s&limit=20'%req_v)]
                        except:
                                self.write(memcache.get(req_v+'v'))
                                return
                        if req_ajax=='1':
                                ret=json.dumps(arts,ensure_ascii=False).replace('title','t').replace('id','i')
                                memcache.set(req_c+'c',ret,6000)
                                self.write(ret)
                        else:
                                self.write(json.dumps(arts,ensure_ascii=False))
                else : 
                        #self.redirect("/c")
                        isGetArts=True
                        listId=''
                        #c={'title':'CnBeta 触屏版','curname':'当前页','nextname':'推荐','nextpage':'cb?t=1'}
                        #self.response.headers['Cache-Control']='no-store'
                       #c['prename']='精彩评论'
                       #c['prepage']='cb?com=1'
                        #先从api中抓
                        arts=Gae.urljson('http://www.cnbeta.com/api/getNewsList.php?limit=20')
                        #id越大表示越新
                        if arts:  
                                arts=Gae.urljson('http://www.cnbeta.com/api/getNewsList.php?limit=20&fromArticleId=%d'%((int(arts[0]["ArticleID"]))+10))
                        if arts:
                                listId=arts[19]["ArticleID"];
                        else:
                                listId='0'
                                isGetArts=False
                                arts=[]
                        #从网页获取
                        result=Gae.urlfind('http://www.cnbeta.com','<dt class="topic" ><a href="/articles/(.*?)\.htm".*?target="_blank"><strong>(.*?)</strong>')
                        for i in range(len(result)):
                                #比较id,如果比第一个还小就出问题了
                                if isGetArts and cmp(result[i][0],arts[0]["ArticleID"])<1:
                                        break
                                #网页是从大到小
                                arts.insert(i,{"i":result[i][0],'t':Gae.str2u(result[i][1]),'cmtnum':'0','cmtClosed':'0'})
                        if len(arts)<1:
                                self.write(memcache.get("index"))
                                return		
                        #if req_ajax=='1':
                        ret=json.dumps([{"t":art["t"]+'('+{'0':art["cmtnum"],'1':u'关'}[art["cmtClosed"]]+')',"i":int(art["i"])} for art in arts],ensure_ascii=False)
                        memcache.set("index",ret,600)
                        self.write(ret)
        def post(self):
                #负责评论
                req_i=self.getquery('i')
                req_m=self.getquery('m')
                req_c=self.getquery('c')
                req_t=self.getquery('t')
                if req_t=='':
                        req_t='0'
                result=Gae.url('http://cnbeta.com/Ajax.comment.php?ver=new',payload=Gae.encode({'comment':req_c,'sid':req_i,'tid':req_t,'valimg_main':req_m}),method='POST',headers={'Referer':'http://www.cnbeta.com','Cookie':'PHPSESSID=1'})
                self.write(result[0])
def main():
        return Gae.run([('/cb',Cnbeta)])
#if __name__=='__main__':
app=main()
